#include "volumetool.h"
#include "ui_volumetool.h"

#include <QGraphicsDropShadowEffect>
#include <QPainter>
#include <QDebug>

VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),
    isMuted(false)
{
    ui->setupUi(this);

    //设置背景透明
    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    //设置阴影效果
    QGraphicsDropShadowEffect *dropShadowEffect = new QGraphicsDropShadowEffect(this);
    dropShadowEffect->setColor("#646464");
    dropShadowEffect->setBlurRadius(10);
    dropShadowEffect->setOffset(0, 0);
    this->setGraphicsEffect(dropShadowEffect);

    //设置图标
    ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));

    //设置默认音量为20%
    //设置outLine
    ui->volumeRatio->setText("20%");
    QRect rect = ui->outLine->geometry();
    ui->outLine->setGeometry(rect.x(), rect.y() + 180 - 36, rect.width(), 36);
    //设置按钮
    ui->sliderBtn->move(ui->sliderBtn->x(), ui->outLine->y() - ui->sliderBtn->width()/2);

    //静音处理
    connect(ui->silenceBtn, &QPushButton::clicked, this, &VolumeTool::onSilenceBtnClicked);

    //添加事件过滤器
    ui->volumeBox->installEventFilter(this);

}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::paintEvent(QPaintEvent *event)
{
    //绘制volumeTool界面下的三角
    QPainter painter(this);

    //设置画笔
    painter.setPen(Qt::NoPen);

    //设置笔刷
    painter.setBrush(QBrush(Qt::white));

    //设置三角形
    QPolygon polygon;
    QPoint a(10 + 30, 10 + 300);
    QPoint b(10 + 50, 10 + 300);
    QPoint c(10 + 40, 10 + 300 + 10);
    polygon.append(a);
    polygon.append(b);
    polygon.append(c);

    //绘制
    painter.drawPolygon(polygon);
}

void VolumeTool::onSilenceBtnClicked()
{
    isMuted = !isMuted;

    //设置图标
    if(isMuted)
    {
        ui->silenceBtn->setIcon(QIcon(":/images/silent.png"));
    }
    else
    {
        ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));
    }

    //发送信号给MusicPlayer,处理是否静音
    emit setMusicMuted(isMuted);
}

bool VolumeTool::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->volumeBox)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
            //鼠标点击
            //计算音量
            calcVolume();
            qDebug()<<"11111";
        }
        else if(event->type() == QEvent::QEvent::MouseButtonRelease)
        {
            //鼠标抬起
            //发送信号给MusicPlayer,设置player音量
            emit setPlayerVolume(volume);
        }
        else if(event->type() == QEvent::QEvent::MouseMove)
        {
            //鼠标移动
            //计算音量
            calcVolume();
            //发送信号给MusicPlayer,设置player音量
            emit setPlayerVolume(volume);
        }

        return true;
    }

    return QObject::eventFilter(watched, event);
}

void VolumeTool::calcVolume()
{
    //获取鼠标相对于volumeBox的y坐标
    int y = ui->volumeBox->mapFromGlobal(QCursor().pos()).y();

    //使y在合法范围[25, 205]
    y = y < 25 ? 25 : y;
    y = y > 205 ? 205 : y;

    //设置outLine的相应位置
    ui->outLine->setGeometry(ui->outLine->x(), y, ui->outLine->width(), 205 - y);

    //设置sliderBtn的相应位置
    ui->sliderBtn->move(ui->sliderBtn->x(), y - ui->sliderBtn->height()/2);

    //计算音量
    volume = (int)(ui->outLine->height()/(float)180*100);

    //设置音量百分比图标
    ui->volumeRatio->setText(QString::number(volume) + '%');
}
